<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=1000, initial-scale=1.0, maximum-scale=1.0">
	    <!-- Loading Bootstrap -->
	    <link href="./Public/admin/Flat/dist/css/vendor/bootstrap.min.css" rel="stylesheet">
	    <!-- Loading Flat UI -->
	    <link href="./Public/admin/Flat/dist/css/flat-ui.css" rel="stylesheet">
	    <!--<link href="./Public/admin/Flat/docs/assets/css/demo.css" rel="stylesheet">-->
	    <link rel="shortcut icon" href="./Public/admin/Flat/img/favicon.ico">
	    <script src="./Public/uploadify/jquery-1.8.2.min.js" type="text/javascript" charset="utf-8"></script>
	    <script src="./Public/uploadify/jquery.uploadify.min.js" type="text/javascript" charset="utf-8"></script>
	    <link rel="stylesheet" type="text/css" href="./Public/uploadify/uploadify.css"/>
	     <!--载入百度编辑器-->
	    	<script type="text/javascript" charset="utf-8" src="Public/admin/ueditor1_4_3/ueditor.config.js"></script>
    		<script type="text/javascript" charset="utf-8" src="Public/admin/ueditor1_4_3/ueditor.all.min.js"> </script>
    		<script type="text/javascript" charset="utf-8" src="Public/admin/ueditor1_4_3/lang/zh-cn/zh-cn.js"></script>
	<script type="text/javascript">
		$(function(){
			$("#cate").change(function(){
				var tid=$('option:selected').attr('tid');
				//添加tid隐藏域
				var input='';
				input+='<input type="hidden" name="shop_type_tid"  value="'+tid+'" />';
				$('#form').append(input);
				//异步添加
				$.ajax({
					type:'post',
					url:"{{U('Goods/addAttr')}}",
					data:{t:tid},
					dataType:'json',
					success:function(phpData){
//						console.log(phpData);
						//组合字符串
						//属性字符串
						var attr='';
						//规格字符串
						var spec='';
						//类似foreach的jquery  each循环处理
						$.each(phpData, function(k,v) {
							//添加属性/规格
							if(v.class==0){
								attr+='<tr>';
								attr+='<td>'+v.taname+'</td>';
					 			attr+='<td>';
					 			attr+='<select name="attr['+v.taid+']" class="form-control" id="">';
					 			attr+='<option value="">请选择</option>'	;
					 			$.each(v.tavalue,function(kk,vv){
					 				attr+='<option value="'+vv+'">'+vv+'</option>'
					 			});
								attr+='</select></td></tr>';
							}else{//添加规格
								spec+='<tr>';
								spec+='<td>'+v.taname+'</td>';
					 			spec+='<td>';	
					 			spec+='<select name="spec[value]['+v.taid+'][]" class="form-control" id="">';
					 			spec+='<option value="">请选择</option>'	;	
								$.each(v.tavalue,function(kk,vv){
									spec+='<option value="'+vv+'">'+vv+'</option>'
								})
								spec+='</select></td>';			
								spec+='<td>附加价格</td><td>'	;
								spec+='<input type="text" name="spec[gaadd]['+v.taid+'][]"/></td> ';	
								spec+='<td><button type="button" class="btn btn-primary addSpec">添加规格</button></td></tr>';
							}
						});
						$('#attr').html(attr);
						$('#spec').html(spec);
					}
				});	
			});
			//点击添加规格
			$('.addSpec').live('click',function(){
				//找到当前的父级tr
				var trP=$(this).parents('tr');
				//克隆一个tr
				var tr=trP.clone();
				//替换掉button的值
				tr.find('.addSpec').html("删除规格").removeClass('btn-primary addSpec').addClass('btn-info delSpec');
				trP.after(tr);
			});
			//点击删除规格
			$('.delSpec').live('click',function(){
				$(this).parents('tr').remove();
			});
		})	
	</script>
	</head>
	
	<body>
		<form action="" method="post" id="form" enctype="multipart/form-data">
		<div class="alert alert-success">添加商品</div>
		<div class="alert bg-primary">基本信息</div>
		<input type="hidden" name="shop_admin_aid" value="{{$_SESSION['info']['aid']}}" />
		
		<div class="form-group">
			<label for="exampleInputEmail1">所属分类</label>
			<select name="shop_category_cid" class="form-control" id="cate" >
				<option value="">请选择分类</option>
				{{foreach from="$cate" value="$c"}}
				<option value="{{$c['cid']}}"  tid="{{$c['shop_type_tid']}}">{{$c['_name']}}</option>
				{{endforeach}}
			</select>

		</div>
		<div class="form-group">
			<label for="exampleInputEmail1">所属品牌</label>
			<select name="shop_brand_bid" class="form-control" id="cate">
				<option value="0">请选择品牌</option>
				{{foreach from="$brand" value="$b"}}
				<option value="{{$b['bid']}}">{{$b['bname']}}</option>
				{{endforeach}}
			</select>
		</div>
		<div class="form-group">
			<label for="exampleInputEmail1">商品名称</label>
			<input id="exampleInputEmail1" class="form-control" type="text" placeholder="请输入商品名称"  name="gname">
		</div>
		<div class="form-group">
			<label for="exampleInputEmail1">货号</label>
			<input id="exampleInputEmail1" class="form-control" type="text" placeholder="请输入货号"  name="gnumber">
		</div>
		<div class="form-group">
			<label for="exampleInputEmail1">单位</label>
			<input id="exampleInputEmail1" class="form-control" type="text" placeholder="请输入计算单位"  name="uint">
		</div>
		<div class="form-group">
			<label for="exampleInputEmail1">市场价</label>
			<input id="exampleInputEmail1" class="form-control" type="text" placeholder="请输入市场价"  name="makertprice">
		</div>
		<div class="form-group">
			<label for="exampleInputEmail1">商城价</label>
			<input id="exampleInputEmail1" class="form-control" type="text" placeholder="请输入商城价"  name="shopprice">
		</div>
		
		<div lab="uploadFile">
			<label for="exampleInputEmail1">列表图</label><br />
			<!-- file表单 -->
		    <input id="f" type="file" multiple="true">
		    <span>类型: *.jpg,*.png 大小: 2000KB 数量: 10</span>
		    <script type="text/javascript">
		        $(function() {
		            $('#f').uploadify({
		                'formData'     : {//POST数据
		                    '<?php echo session_name();?>' : '<?php echo session_id();?>',
		                },
		                'fileTypeExts' : '*.jpg;*.png',
		                'swf'      : '{{__PUBLIC__}}/uploadify/uploadify.swf',
		                'uploader' : '{{U('upload')}}',//指定服务器上传的方法
		                'buttonText':'选择文件',
		                'fileSizeLimit' : '2000KB',
		                'uploadLimit' : 1,//上传文件数
		                'width':65,
		                'height':25,
		                'successTimeout':10,//等待服务器响应时间
		                'removeTimeout' : 0.2,//成功显示时间
		                'onUploadSuccess' : function(file, data, response) {
		                    //把php返回的数据转为json
		                    data=$.parseJSON(data);
		                    //获得图片地址
		                    var imageUrl = data.url;
		                    var li="<li>";
		                    li += "<img src='"+imageUrl+"'/>";
		                    li += "<input type='hidden' name='pic' id='pic' value='"+data.path+"'/><a href='javascript:;' id='self-close'>X</a>";
		                    li += "</li>";
		                    $("#uploadList ul").prepend(li);
		                }
		            });
		
					//关闭图片
		            var i = 1;
		            $('#self-close').live('click',function(){
		            	var pic=$("#pic").attr('value');
		            	$.ajax({
		                	type:'post',
		                	url:"{{U('Goods/delUpload')}}",
		                	data:{path:pic}
		               });
		                
		                $(this).parent('li').remove();
		                i++;
		                $('#f').uploadify('settings','uploadLimit',i);
		                
		            })
		        });
		    </script>
		    <div id="uploadList">
		        <ul>
		
		        </ul>
		    </div>
		</div>
		<div class="alert bg-primary">商品属性</div>
		<table class="table table-bordered" id="attr">
 			
		</table>
		<div class="alert bg-primary">商品规格</div>
		<table class="table table-bordered" id="spec">
 			
		</table>
		<div class="alert bg-primary">商品详情</div>
		<div lab="uploadFile">
			<label for="exampleInputEmail1">商品详情图</label><br />
			<!-- file表单 -->
		    <input id="d" type="file" multiple="true">
		    <span>类型: *.jpg,*.png 大小: 2000KB 数量:5</span>
		    <script type="text/javascript">
		        $(function() {
		        	var x=0;
		            $('#d').uploadify({
		                'formData'     : {//POST数据
		                    '<?php echo session_name();?>' : '<?php echo session_id();?>',
		                },
		                'fileTypeExts' : '*.jpg;*.png',
		                'swf'      : '{{__PUBLIC__}}/uploadify/uploadify.swf',
		                'uploader' : '{{U('upload')}}',//指定服务器上传的方法
		                'buttonText':'选择文件',
		                'fileSizeLimit' : '2000KB',
		                'uploadLimit' : 6,//上传文件数
		                'width':65,
		                'height':25,
		                'successTimeout':10,//等待服务器响应时间
		                'removeTimeout' : 0.2,//成功显示时间
		                'onUploadSuccess' : function(file, data, response) {
		                    //把php返回的数据转为json
		                    data=$.parseJSON(data);
		                    //获得图片地址
		                    var imageUrl = data.url;
		                    		if(x>=6){x=0;}
			                    	var li="<li>";
				                    li += "<img src='"+imageUrl+"'/>";
				                    li += "<input type='hidden' name='dimg[]' id='dimg"+x+"' value='"+data.path+"'/><a href='javascript:;' id='self-clos"+x+"'>X</a>";
				                    li += "</li>";
				                    x++;
		                    $("#uploadLis ul").prepend(li);
		                }
		            });
						<?php $num=array('0'=>'0','1'=>'1','2'=>'2','3'=>'3','4'=>'4','5'=>'5') ?>
						//关闭图片
			            var i = 5;
			          	<?php foreach($num as $v): ?>
			            	 $('#self-clos{{$v}}').live('click',function(){
					           	var dimg=$("#dimg{{$v}}").attr('value');
					           	$.ajax({
					               	type:'post',
					               	url:"{{U('Goods/delUpload')}}",
					               	data:{path:dimg}
					              });
					            $(this).parent('li').remove();
					            i++;
					            $('#d').uploadify('settings','uploadLimit',i);
			            	});
			            <?php endforeach?>
		        });
		    </script>
		    <div id="uploadLis">
		        <ul>
		
		        </ul>
		    </div> 
		    <div class="form-group">
				<label for="exampleInputEmail1">包装清单</label>
				<!--调用百度编辑器-->
				<script id="editor" type="text/plain" style="width:100%;height:200px;" name="dlist"></script>
			    <script>
			        var ue = UE.getEditor('editor');
			    </script>
			</div>
			 <div class="form-group">
				<label for="exampleInputEmail1">商品详情</label>
				<!--调用百度编辑器-->
				<script id="detail" type="text/plain" style="width:100%;height:400px;" name="detail"></script>
			    <script>
			        var ue = UE.getEditor('detail');
			    </script>
			</div>
		<button class="btn btn-primary btn-block" type="submit"> 确定 </button>
		</form>
	</body>
</html>
